<template>
  <el-dialog
    :title="!dataForm.orderId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="订单编号" prop="orderNumber">
      <el-input v-model="dataForm.orderNumber" placeholder="订单编号"></el-input>
    </el-form-item>
    <el-form-item label="订单类型 1出租 2新房 3二手房" prop="orderType">
      <el-input v-model="dataForm.orderType" placeholder="订单类型 1出租 2新房 3二手房"></el-input>
    </el-form-item>
    <el-form-item label="订单价格" prop="orderPrice">
      <el-input v-model="dataForm.orderPrice" placeholder="订单价格"></el-input>
    </el-form-item>
    <el-form-item label="订单名称" prop="orderName">
      <el-input v-model="dataForm.orderName" placeholder="订单名称"></el-input>
    </el-form-item>
    <el-form-item label="订单购买人" prop="orderUserid">
      <el-input v-model="dataForm.orderUserid" placeholder="订单购买人"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="orderTimes">
      <el-input v-model="dataForm.orderTimes" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态 1未支付 2支付完成 3订单取消" prop="orderStatus">
      <el-input v-model="dataForm.orderStatus" placeholder="订单状态 1未支付 2支付完成 3订单取消"></el-input>
    </el-form-item>
    <el-form-item label="房产id" prop="orderHouseid">
      <el-input v-model="dataForm.orderHouseid" placeholder="房产id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          orderId: 0,
          orderNumber: '',
          orderType: '',
          orderPrice: '',
          orderName: '',
          orderUserid: '',
          orderTimes: '',
          orderStatus: '',
          orderHouseid: ''
        },
        dataRule: {
          orderNumber: [
            { required: true, message: '订单编号不能为空', trigger: 'blur' }
          ],
          orderType: [
            { required: true, message: '订单类型 1出租 2新房 3二手房不能为空', trigger: 'blur' }
          ],
          orderPrice: [
            { required: true, message: '订单价格不能为空', trigger: 'blur' }
          ],
          orderName: [
            { required: true, message: '订单名称不能为空', trigger: 'blur' }
          ],
          orderUserid: [
            { required: true, message: '订单购买人不能为空', trigger: 'blur' }
          ],
          orderTimes: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          orderStatus: [
            { required: true, message: '订单状态 1未支付 2支付完成 3订单取消不能为空', trigger: 'blur' }
          ],
          orderHouseid: [
            { required: true, message: '房产id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.orderId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.orderId) {
            this.$http({
              url: this.$http.adornUrl(`/house/houseorder/info/${this.dataForm.orderId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.orderNumber = data.houseOrder.orderNumber
                this.dataForm.orderType = data.houseOrder.orderType
                this.dataForm.orderPrice = data.houseOrder.orderPrice
                this.dataForm.orderName = data.houseOrder.orderName
                this.dataForm.orderUserid = data.houseOrder.orderUserid
                this.dataForm.orderTimes = data.houseOrder.orderTimes
                this.dataForm.orderStatus = data.houseOrder.orderStatus
                this.dataForm.orderHouseid = data.houseOrder.orderHouseid
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/house/houseorder/${!this.dataForm.orderId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'orderId': this.dataForm.orderId || undefined,
                'orderNumber': this.dataForm.orderNumber,
                'orderType': this.dataForm.orderType,
                'orderPrice': this.dataForm.orderPrice,
                'orderName': this.dataForm.orderName,
                'orderUserid': this.dataForm.orderUserid,
                'orderTimes': this.dataForm.orderTimes,
                'orderStatus': this.dataForm.orderStatus,
                'orderHouseid': this.dataForm.orderHouseid
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
